const qiniu = require('qiniu');
const path = require('path');
const fs = require('fs');
class UploadPlugin {
    constructor(options) {
        this.options = options || {};
    }
    apply(compiler) {
        compiler.hooks.done.tap('UploadPlugin', (compilation) => {
            let { bucket = 'video', domain = "img.zhufengpeixun.cn", accessKey = 'fi5imW04AkxJItuFbbRy1ffH1HIoo17HbWOXw5fV', secretKey = 'ru__Na4qIor4-V7U4AOJyp2KBUYEw1NWduiJ4Pby' } = this.options;
            var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
            var options = {
                scope: bucket,
            };
            var putPolicy = new qiniu.rs.PutPolicy(options);
            var uploadToken = putPolicy.uploadToken(mac);
            var config = new qiniu.conf.Config();

            console.log(compilation.assets);

            let promises = Object.keys(compilation.assets).map(asset => upload(asset));
            Promise.all(promises).then(function (data) {
                console.log('发布CDN成功!');
            });

            function upload(filename) {
                return new Promise(function (resolve, reject) {
                    var localFile = path.join(__dirname, '../build', filename);
                    var formUploader = new qiniu.form_up.FormUploader(config);
                    var putExtra = new qiniu.form_up.PutExtra();
                    var key = filename;
                    formUploader.putFile(uploadToken, key, localFile, putExtra, function (err, body, info) {
                        if (err)
                            reject(err);
                        else
                            resolve(body)
                    });
                });
            }
        });
    }
}
module.exports = UploadPlugin;